import {
  examineList,
  examine,
  examineCount
} from '@api/pattern/index.js'
import { postAction } from '@/api/manage'
import { pattern } from '@api/pattern/index'
import { getSystemPro } from '@api/pattern/index.js'


const riskList = [{
    title: '原创／著作问题',
    value: "10",
  },
  {
    title: '社会和谐',
    value: "20",
  },
  {
    title: '政治敏感',
    value: "30",
  },
  {
    title: '文化歧视',
    value: "40",
  },
  {
    title: '语言问题',
    value: "50",
  },
  {
    title: '未成年问题',
    value: "60",
  },
]
export const listMixin = {
  data() {
    return {
      riskList,
      risks: [],
      text: '',
      patternCode: '',
      order: [0, 10, 20],
      current: 1,
      current1: 1,
      current2: 1,
      current3: 1,
      brand:undefined,
      season:undefined,
      spinning: false,
      examineRemark: '',
      showExamineRemark: '',
      riskFlag: '',
      size: 1,
      total: '',
      showNext: true,
      patternImg: '',
      showButton:true,
      colorList: [],
      colorLength: '',
      whetherHasEnglish: '',
      code: '',
      form: this.$form.createForm(this),
      patternName: '',
      remarks: '',
      visible: false,
      showAll: false,
      ifTag:0,
      showHis: false,
      operation: '',
      elementList:[],
      patternId: '',
      showY:false,
      meaning: '',
      defaultImg: 'https://img.bzhz.jnbygroup.com/cloth.png',
      showSkc: true,
      imgVisible: false,
      imgElementVisible: false,
      elementImageUrl: '',
      bandList: [],
      patternList: [],
      key: '0',
      prevImageUrl: []
    }
  },

  computed: {
    jobNumber() {
      return this.$store.getters.userInfo.workNo || ''
    },
  },

  mounted() {
    this.getCount()
    this.getBrandList()
    this.getsysPro()
  },
  methods: {
    prevElementImg(url) {
      if (typeof url == 'string' && url != '') {
        this.elementImageUrl = url
        this.imgElementVisible = true
      }
    },
    prevImg(url, urlback, index) {
      this.prevImageUrl = []
      if (typeof url == 'string' && url != '') {
        this.prevImageUrl.push(url)
        this.prevImageUrl.push(urlback)
        this.imgVisible = true
      }
      this.$nextTick(() => {
        this.$refs['varousel2'].goTo(index)
      })
    },
    showBigPhoto(index) {
      this.prevImageUrl = []
      this.colorList.map((item) => {
        this.prevImageUrl.push(item.images[0].url)
      })
      this.imgVisible = true
      this.$nextTick(() => {
        this.$refs['varousel2'].goTo(index)
      })
    },
    bigImg(index) {
      this.prevImageUrl = []
      this.colorList.map((item) => {
        this.prevImageUrl.push(item.images[0].url)
      })
      this.imgList = this.prevImageUrl
      this.goToIndex = index
      setTimeout(() => {
        this.visibleImgs = true
      }, 100)
    },
    async getsysPro() {
      try {
        const res = await getSystemPro('season')
        this.seasons = res.data || []
        this.seasons = this.seasons.sort((a, b) => {
          return b.code.localeCompare(a.code, 'zh-CN')
        })
        console.log(this.seasons)
      } catch (error) {
        console.log(error)
      }
    },
    async getBrandList() {
      const data = {
        jobNumber: this.jobNumber
      }
      const res = await getSystemPro('brand', data)
      if (res.code == 200) {
        this.brandList = res.data
        this.brandList = this.brandList.sort((a, b) => {
          const arr = ['JNBY', '速写', 'LESS', 'jnby by JNBY', 'jnby for mini', 'Pomme de terre', 'A PERSONAL NOTE 73', 'JNBY HOME']
          return arr.indexOf(a.code) - arr.indexOf(b.code)
        })
      } else {
        this.$message.error(res.msg || res.message)
      }
    },

    selectBrand(e) {
      this.brand = e
    },
    selectSeason(e) {
      this.season = e
    },
    showModal() {
      this.visible = true
    },
    showHistory(key) {
      this.showHis = true
      if(key ==1){
        this.ifTag = 1
      }
      
    },
    closeHistory() {
      this.showHis = false
      this.ifTag = 0
    },
    
    showYidun() {
      this.showY = true
    },
    closeYidun() {
      this.showY = false
    },
    handleSubmit(e) {
      e.preventDefault()
      this.current1 == 1
      this.current2 == 1
      this.current3 == 1
      this.getCount(1)
    },
    resetForm(e) {
      e.preventDefault()
      this.patternName = ''
      this.patternCode = ''
      this.text = ''
      this.getCount()
    },
    handleCancel(e) {
      this.visible = false
      this.examineRemark = ''
    },
    last() {
      if (this.key == 0) {
        this.current1 -= 1
      } else if (this.key == 10) {
        this.current2 -= 1
      } else {
        this.current3 -= 1
      }
      this.getList(this.key,7)
      this.$refs['varousel'].goTo(0)
    },
    next() {
      console.log(this.key);
      console.log(this.current1);
      if (this.key == 0) {
        this.current1 += 1
      } else if (this.key == 10) {
        this.current2 += 1
      } else {
        this.current3 += 1
      }
      this.getList(this.key,9)
      this.$refs['varousel'].goTo(0)
    },
    async getCount(key) {
      if (key) {
        this.count1 = 0
        this.count2 = 0 
        this.count3 = 0
        this.current1 = 1
        this.current2 = 1
        this.current3 = 1
      }
      const data = {
        jobNumber: this.jobNumber,
        brand: this.brand,
        season: this.season,
        groupNameList:['设计组'],
        relationSkcFlag:2,
        patternName: this.patternName,
        patternCode: this.patternCode,
      }
      try {
        const res = await examineCount('', data)
        if (res.code == 200) {
          res.data.map(item => {
            if (item.examineStatus == 0) {
              this.count1 = item.count
            } else if (item.examineStatus == 10) {
              this.count2 = item.count
            } else if (item.examineStatus == 20) {
              this.count3 = item.count
            }
          })
          this.getList(this.key, 8)
        } else {
          this.$message.warning(res.msg)
        }
      } catch (error) {
       
      }
    },
    async getList(key, type) {
      this.spinning = true;
      this.key = key ? key : 0
      this.risks = []
      const data = {
        size: this.size,
        current: this.key == 0 ? this.current1 : this.key == 10 ? this.current2 : this.current3,
        brand: this.brand,
        season: this.season,
        patternName: this.patternName,
        patternCode: this.patternCode,
        examineStatus: this.key,
        jobNumber: this.jobNumber,
        relationSkcFlag:2,
        groupNameList:['设计组'],
      }
      try {
        const res = await examineList('', data)
        if (res.code == 200) {
          this.current = res.data.current
          if (!res.data.records[0]) {
            console.log(1111);
            if (type == 8) {
              this.showAll = false
              if (res.data.current == 1 && this.key == 0) {
                this.count1 = 0
                this.current1 = 1
              } else if (res.data.current == 1 && this.key == 10) {
                this.count2 = 0
                this.current2 = 1
              } else if (res.data.current == 1 && this.key == 20) {
                this.count3 = 0
                this.current3 = 1
              }
              if (this.key == 0) {
                this.current1 -= 1
              } else if (this.key == 10) {
                this.current2 -= 1
              } else if (this.key == 20) {
                this.current3 -= 1
              }
              if (this.current != 1) {
                this.getList(this.key)
                this.current -= 1
              } else {
                this.patternId = ''
                this.$message.warning('没有更多了')
              }
            } else {
              this.patternId = ''
              this.$message.warning('没有更多了')
              this.current -= 1
              if (this.key == 0) {
                this.current1 -= 1
              } else if (this.key == 10) {
                this.current2 -= 1
              } else {
                this.current3 -= 1
              }
            }
            // this.last()
          } else {
            this.bandLeft = [];
            this.bandRight = []
            this.showAll = true
            this.id = res.data.records[0].id
            if (res.data.records[0].brandCode === '速写') {
              this.code = 'CROQUIS'
            } else if (res.data.records[0].brandCode === 'A PERSONAL NOTE 73') {
              this.code = 'APN 73'
            } else if (res.data.records[0].brandCode === 'jnby by JNBY') {
              this.code = '童装'
            } else if (res.data.records[0].brandCode === 'Pomme de terre') {
              this.code = '蓬马'
            } else if (res.data.records[0].brandCode === 'jnby for mini') {
              this.code = '婴童'
            } else {
              this.code = res.data.records[0].brandCode
            }
            this.total = res.data.total
            this.patternId = res.data.records[0].id
            this.patternList = res.data.records[0]
            this.whetherHasEnglish = res.data.records[0].whetherHasEnglish == 1 ? '是' : '否'
            this.riskFlag = res.data.records[0].riskFlag == 1 ? '是' : '否'
            this.colorList = res.data.records[0].colors
            this.colorLength = res.data.records[0].colors.length
            this.patternImg = res.data.records[0].colors[0].images.length ? res.data.records[0].colors[0].images[0].url : ''
            this.showExamineRemark = res.data.records[0].examineRemark
            this.patternList.risk.map((item) => {
              this.riskList.map((riskitem) => {
                if (item == riskitem.value) {
                  this.risks.push(riskitem)
                }
              })
            })
            this.getElementList()
          }
          this.$refs['varousel'].goTo(0)

        } else {
          console.log(222);
          if(type == 9){
            if (this.key == 0) {
              this.current1 -= 1
            } else if (this.key == 10) {
              this.current2 -= 1
            } else {
              this.current3 -= 1
            }
          }
          if(type == 7){
            if (this.key == 0) {
              this.current1 += 1
            } else if (this.key == 10) {
              this.current2 += 1
            } else {
              this.current3 += 1
            }
          }
          this.$message.warning(res.msg)
        }
      } catch (error) {
console.log(333);
        if(type == 9){
          if (this.key == 0) {
            this.current1 -= 1
          } else if (this.key == 10) {
            this.current2 -= 1
          } else {
            this.current3 -= 1
          }
        }
        if(type == 7){
          if (this.key == 0) {
            this.current1 += 1
          } else if (this.key == 10) {
            this.current2 += 1
          } else {
            this.current3 += 1
          }
        }
      } finally {
        this.spinning = false;
      }

    },
    getElementList(){
      const data = {
        patternId:this.patternId
      }
      postAction(`${pattern}element/list/by/pattern`, data).then(res => {
        if(res.code == 200){
          this.elementList = res.data
          console.log(this.elementList);
        }
      })
    },
    async pass(operation) {
      if (operation == '20' && this.examineRemark == '') {
        this.$message.warning('请输入批注')
      } else {
        const data = {
          id: this.id,
          jobNumber: this.jobNumber,
          examineRemark: this.examineRemark,
          operation: operation,
        }
        try {
          this.spinning = true
          const res = await examine('', data)
          if (res.code == 200) {
            this.$message.success('操作成功,跳转下一个')
            this.handleCancel()
            this.getCount()
          } else {
            this.$message.warning(res.msg || res.message)
          }
        } catch (error) {
        }finally {
          this.spinning = false;
        }
      
      }
    },
  },
}